package com.team.party.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.team.party.bean.Manager;
import com.team.party.bean.Society;
import com.team.party.bean.Student;
import com.team.party.service.ManagerService;
import com.team.party.service.StudentService;

@RestController
@RequestMapping("/user")
public class loginController {
    
	@Autowired
    private StudentService studentservice;
    @Autowired
    private ManagerService managerservice;
    
    /**
     * @throws Exception 
     * @Description:用户登录
     */
    @RequestMapping("/login")
    public String login(
    	@RequestParam(value = "username", required = false) String username,
		@RequestParam(value = "password", required = false) String password,
		HttpSession httpSession) throws Exception{
    	
    	System.out.println("能进来"+username+password);
    	
    	Student student = studentservice.serchStudent(username);
    	if(student !=null ) {
    		//在学生表中查找到该用户名
    		if(password.equals(student.getStu_password())) {
    			//学生登录成功
    			httpSession.setAttribute("loginStudent", student);
    			System.out.println("+++++++++++++++++++");
    			return "Stu";   			
    		} else {
    			//学生密码错误
    			return "学生密码错误";
    		}
    	} else {
    		//学生表中不存在，到管理员表中查询
    		Manager manager = managerservice.serchManager(username);
    		
    		if(manager != null) {
    			//在管理员表中查到该用户名
    			if(password.equals(manager.getMan_password())) {
    				//管理员登录成功，实例化对应的社团，并加入session
    				Society society = managerservice.serchMySociety(manager.getSoc_no());
    				httpSession.setAttribute("loginManager", society);
    				System.out.println("+++++++++++"+httpSession.getAttribute("loginManager"));
    				return "Man";
    			} else {
    				//管理员密码错误
    				return "管理员密码错误";
    			}
    		} else {
    			//管理员表中仍不存在
    			//该 用户名 不存在
    			return "该用户名不存在";
    		}
    	}
    }
    
    
    //管理拿数据
    @ResponseBody
	@RequestMapping("/getManUser")
	public Society getManUser(HttpSession httpSession) {
	    //取session里的内容
    	Society society = (Society)httpSession.getAttribute("loginManager");
		System.out.println("////////////////////"+society);
		return society;
	}
    //学生拿数据
    @ResponseBody
	@RequestMapping("/getStuUser")
	public Student getStuUser(HttpSession httpSession) {
	    //取session里的内容
		Student user = (Student) httpSession.getAttribute("loginStudent");
		return user;
	}
    
}
